www.gusucode.com > 网页游戏五子棋onWeb 2008asp网站源码程序 > 网页游戏五子棋onWeb 2008asp网站源码程序/five/db/phpsql.php
<?php /**********************************[mickie opus]*************************************/ //(c) 2007, code by mickie, LU october 28 2007, CSUST Changsha Hunan China. /**********************************[mickie opus]*************************************/ $phpsql_user = "root"; $phpsql_password = "root"; $phpsql_loc = ""; $phpsql_db = ""; function phpsql_show($str){ $str = StripSlashes($str); $str = str_replace("\", "\\", $str); $str = str_replace("/", "/", $str); $str = str_replace(" ", " ", $str); $str = str_replace(",", ",", $str); return $str; } function phpsql_post($str){ $str = StripSlashes($str); $str = str_replace("|", "|", $str); $str = str_replace("<", "<", $str); $str = str_replace(">", ">", $str); $str = str_replace(" ", " ", $str); $str = str_replace(" ", " ", $str); $str = str_replace("(", "(", $str); $str = str_replace(")", ")", $str); $str = str_replace("`", "`", $str); //$str = str_replace("'", "'", $str); $str = str_replace('"', """, $str); $str = str_replace(",", ",", $str); $str = str_replace("$", "$", $str); $str = str_replace("\\", "\", $str); $str = str_replace("/", "/", $str); return $str; } function phpsql_replace($str){ $str = StripSlashes($str); $str = str_replace("'", "'", $str); return $str; } function phpsql_connect($location, $username, $password){ global $phpsql_loc, $phpsql_user, $phpsql_password; if($phpsql_user != $username || $phpsql_password != $password) phpsql_error("cannot access to PHPSQL!(wrong username/wrong password)"); define(PHPSQL_LOC, $location); $phpsql_loc = $location; } function phpsql_select_db($db){ global $phpsql_db; define(PHPSQL_DB, $db); $phpsql_db = $db; } function phpsql_list_table(){ global $phpsql_loc, $phpsql_db; $i = 0; if(!($handle = @opendir($phpsql_loc."/".$phpsql_db))) return 0; while(($file = readdir($handle)) !== false){ if($file != "." && $file != "..") { $table[$i ++] = $file; } } return $table; } function phpsql_list_db(){ global $phpsql_loc; $i = 0; if(!$handle = opendir($phpsql_loc)) return 0; while(($file = readdir($handle)) !== false){ if($file != "." && $file != ".." && !strstr($file, ".")) $db[$i ++] = $file; } return $db; } function phpsql_query($query){ global $phpsql_db; $e_query = explode("'", $query); for($i = 1;$i < sizeof($e_query);$i += 2) { $query = str_replace("'".$e_query[$i]."'", phpsql_post("'".$e_query[$i]."'"), $query); } $query_ex = explode(" ", $query); $e_1 = explode("limit ", $query); $limit = $e_1[1]; $e_2 = explode("order by ", $e_1[0]); $order = $e_2[1]; $e_3 = explode("where ", $e_2[0]); $where = $e_3[1]; $e_4 = explode("set ", $e_3[0]); $will = $e_4[1]; switch($first = strtolower($query_ex[0])) { case alter; if(strstr($query, change)) { $ex = explode(" change ", $query); $table = preg_replace("/alter table `(.+?)`/is", "\\1", $ex[0]); $ex_2 = explode(" default ", $ex[1]); $ex_2_2 = explode("``", $ex_2[0]); $field = str_replace("`", "", $ex_2_2[0]); $new_field = str_replace("`", "", $ex_2_2[1]); $default = str_replace("'", "", $ex_2[1]); return phpsql_altertable($table, $field, $new_field, $default); }elseif(strstr($query, "add index")){ $table = preg_replace("/alter table `(.+?)` add index `(.+?)`/is", "\\1", $query); $index = preg_replace("/alter table `(.+?)` add index `(.+?)`/is", "\\2", $query); return phpsql_addindex($table, $index); }elseif(strstr($query, add)){ $ex = explode(" add ", $query); $table = preg_replace("/alter table `(.+?)`/is", "\\1", $ex[0]); $ex_2 = explode(" default ", $ex[1]); $field = str_replace("`", "", $ex_2[0]); $ex_3 = explode(" ", $ex_2[1]); $default = str_replace("'", "", $ex_3[0]); $add = $ex_3[1]; return phpsql_addfield($table, $field, $default, $add); }elseif(strstr($query, "drop index")){ $table = preg_replace("/alter table `(.+?)` drop index `(.+?)`/is", "\\1", $query); $index = preg_replace("/alter table `(.+?)` drop index `(.+?)`/is", "\\2", $query); return phpsql_dropindex($table, $index); }elseif(strstr($query, drop)){ $table = preg_replace("/alter table `(.+?)` drop `(.+?)`/is", "\\1", $query); $field = preg_replace("/alter table `(.+?)` drop `(.+?)`/is", "\\2", $query); return phpsql_dropfield($table, $field); } break; case drop; if(strstr($query, "drop table")){ $table = preg_replace("/drop table `(.+?)`/is", "\\1", $query); return phpsql_droptable($table); }elseif(strstr($query, "drop database")){ $db = preg_replace("/drop database `(.+?)`/is", "\\1", $query); return phpsql_dropdatabase($db); } break; case create; if(strstr($query, 'create database')) { $table = preg_replace("/create database `(.+?)`/is", "\\1", $query); return phpsql_createdatebase($table); }else{ $phpsql_db = preg_replace("/create table `(.+?)`.`(.+?)` \((.+?)\)/is", "\\1", $query); $table = preg_replace("/create table `(.+?)`.`(.+?)` \((.+?)\)/is", "\\2", $query); $fields = preg_replace("/create table `(.+?)`.`(.+?)` \((.+?)\)/is", "\\3", $query); return phpsql_createtable($table, $fields); } break; case truncate; $table = preg_replace("/truncate table `(.+?)`/is", "\\1", $query); return phpsql_truncate($table); break; case insert; $table = $query_ex[2]; // $str = preg_replace("/\[".$tr."=(.+?)\](.+?)\[\/".$tr."\]/is", $rp, $str); $fields = preg_replace("/(.+?)\((.+?)\) +values+ \((.+?)\)/is", "\\2", $query); $values = preg_replace("/(.+?)\((.+?)\) +values+ \((.+?)\)/is", "\\3", $query); return phpsql_insert($table, $fields, $values); break; case select;case delete; //select * from `table` where `field` = 'value' and `field2` = 'value2' order by `field` desc limit 2,2 //delete from `table` where `field` = 'value' and `field2` = 'value2' order by `field` desc limit 2,2 $ex = explode("from ", $query); $star = preg_replace("/".$first."(.+?)/is", "\\1", $ex[0]); $ex_ex = explode(" ", $ex[1]); $table = $ex_ex[0]; if($first == select) return phpsql_select($star, $table, $where, $order, $limit); elseif($first == delete){ return phpsql_delete($table, phpsql_select($star, $table, $where, $order, $limit)); } break; case update; //$query = "update `table` set `field` = '1',`field2`='2' where `fid` = '1' and `bid` = '2' order by `ID` desc limit 2,2"; $ex = explode(" set ", $query);//update `setting` set `na` = '1' where `ID` = '1' limit 1 $ex_ex = explode(" ", $ex[0]); $table = $ex_ex[1]; return phpsql_update($table, $will, phpsql_select($star, $table, $where, $order, $limit, 'no')); break; default; die($query); break; } } function phpsql_getwill($will){ $ex = explode(",", $will); for($i = 0;$i < sizeof($ex);$i ++) { $ex_ex = explode(" ", $ex[$i]); $arr[$i][field] = str_replace("`", "", $ex_ex[0]); $arr[$i][c] = $ex_ex[1]; $arr[$i][value] = str_replace("'", "", $ex_ex[2]); } return $arr; } function phpsql_delete($table, $arr){ global $phpsql_db; $phpsql_sum = 0; $table = str_replace("`", "", $table); if(strstr($table, ".")){ $table_ex = explode(".", $table); $table_db = $table_ex[0]; phpsql_select_db($table_db); $table = $table_ex[1]; } for($u = 0;$u < sizeof($arr);$u ++) { $arr_all = $arr[$u]; $m = 0; $value_inner = 0; foreach($arr_all as $k => $va) { $p = phpsql_getstructure($table, $k); if($value_inner == 0 && $p[add] == primary) $value_inner = $va; $f[$m] = $k; $v[$m ++] = $va; } if($value_inner == 0) continue; $index = phpsql_getindex($table, $f, $v); for($i = 0;$i < sizeof($index);$i ++) { $target = PHPSQL_LOC."/".$phpsql_db."/".$table."/index/".$index[$i][field].".data.php"; $content = PHPSQL_get_contents($target); $content = preg_replace("/'".$index[$i][value]."','".$value_inner."'\|/is", "", $content); phpsql_write($target, $content); } $target = PHPSQL_LOC."/".$phpsql_db."/".$table."/primary.data.php"; $content = PHPSQL_get_contents($target); $content = preg_replace("/'".$value_inner."'\|/is", "", $content); phpsql_write($target, $content); if(@unlink(PHPSQL_LOC."/".$phpsql_db."/".$table."/data/".$value_inner.".data.php")) $phpsql_sum ++; } return $phpsql_sum; } function phpsql_update($table, $will, $arr){ global $phpsql_db; $phpsql_sum = 0; $table = str_replace("`", "", $table); if(strstr($table, ".")){ $table_ex = explode(".", $table); $table_db = $table_ex[0]; phpsql_select_db($table_db); $table = $table_ex[1]; } $wills = phpsql_getwill($will); for($u = 0;$u < sizeof($arr);$u ++) { $arr_all = $arr[$u]; $m = 0; $value_inner = 0; foreach($arr_all as $k => $va) { $p = phpsql_getstructure($table, $k); if($value_inner == 0 && $p[add] == primary) $value_inner = $va; $f[$m] = $k; $v[$m ++] = $va; } if($value_inner == 0) continue; $flag = 0; for($i = 0;$i < sizeof($wills);$i ++) { if($arr_all[$wills[$i][field]] == $wills[$i][value]) $flag ++; $arr_all[$wills[$i][field]] = $wills[$i][value]; } if(sizeof($wills) == $flag) continue; $m = 0; $f = array(); $v = array(); foreach($arr_all as $k => $va) { $p = phpsql_getstructure($table, $k); if($p[add] == index) { if($arr[$u][$k] != $va) { $target = PHPSQL_LOC."/".$phpsql_db."/".$table."/index/".$k.".data.php"; $content = PHPSQL_get_contents($target); $content = str_replace("'".$arr[$u][$k]."','".$value_inner."'|", "'".$va."','".$value_inner."'|", $content); phpsql_write($target, $content); } } if($p[add] == primary) { if($value_inner != $va){ $target = PHPSQL_LOC."/".$phpsql_db."/".$table."/primary.data.php"; $content = PHPSQL_get_contents($target); $content = str_replace("'".$value_inner."'|", "'".$va."'|", $content); phpsql_write($target, $content); } } $f[$m] = $k; $v[$m ++] = $va; } $target = PHPSQL_LOC."/".$phpsql_db."/".$table."/data/".$value_inner.".data.php"; $content = phpsql_getcontent($f, $v, $ID); if(phpsql_write($target, $content)) $phpsql_sum ++; } return $phpsql_sum; } function phpsql_getallfields($table) { global $phpsql_db; $structure = PHPSQL_get_contents(PHPSQL_LOC."/".$phpsql_db."/".$table."/structure.data.php"); $ex = explode("|", $structure); for($i = 0;$i < sizeof($ex) - 1;$i ++) { //`ID`,'0',(primary) $e_e = explode(",", $ex[$i]); $field_inner = str_replace("`", "", $e_e[0]); $default_inner = str_replace("'", "", $e_e[1]); $add_inner = str_replace("(", "", str_replace(")", "", $e_e[2])); $arr[$i][field] = $field_inner; $arr[$i]['default'] = $default_inner; $arr[$i][add] = $add_inner; } return $arr; } function phpsql_getstructure($table, $field){ global $phpsql_db; $structure = PHPSQL_get_contents(PHPSQL_LOC."/".$phpsql_db."/".$table."/structure.data.php"); $ex = explode("|", $structure); for($i = 0;$i < sizeof($ex) - 1;$i ++) { //`ID`,'0',(primary) $e_e = explode(",", $ex[$i]); $field_inner = str_replace("`", "", $e_e[0]); $default_inner = str_replace("'", "", $e_e[1]); $add_inner = str_replace("(", "", str_replace(")", "", $e_e[2])); if($field == $field_inner) { $arr[field] = $field_inner; $arr['default'] = $default_inner; $arr[add] = $add_inner; return $arr; break; } } } function phpsql_setfv($table, $arr){//form the result global $phpsql_db; if(sizeof($arr[0]) == 0) return null; $content = PHPSQL_get_contents(PHPSQL_LOC."/".$phpsql_db."/".$table."/structure.data.php"); $ex = explode("|", $content); for($j = 0;$j < sizeof($arr);$j ++) { for($i = 0;$i < sizeof($ex) - 1;$i ++){ $ex_ex = explode(",", $ex[$i]); $field = str_replace("`", "", $ex_ex[0]); $default = str_replace("'", "", $ex_ex[1]); $add = str_replace("(", "", str_replace(")", "", $ex_ex[2])); $new_arr[$j][$field] = isset($arr[$j][$field])?$arr[$j][$field]:$default; } } return $new_arr; } function phpsql_getresult($table, $ID, $opentrans = yes){ global $phpsql_db; $content = PHPSQL_get_contents(PHPSQL_LOC."/".$phpsql_db."/".$table."/data/".$ID.".data.php"); $ex = explode("{|n|}", $content); for($i = 0;$i < sizeof($ex);$i ++) { /* $field = preg_replace("/`(.+?)`'(.+?)'/is", "\\1", $ex[$i]); $value = preg_replace("/`(.+?)`'(.+?)'/is", "\\2", $ex[$i]); */ $ex_ex = explode("`'", $ex[$i]); $ex_ex_ex = explode("`", $ex_ex[0]); $field = $ex_ex_ex[sizeof($ex_ex_ex) - 1]; $ex_ex_ex = explode("'", $ex_ex[1]); $value = $ex_ex_ex[0]; if($opentrans == yes) $arr[$field] = phpsql_show($value); else $arr[$field] = $value; } return $arr; } function get_new_c_arr($arr, $c, $value){ switch($c){ case '>'; for($i = 0, $j = 0;$i < sizeof($arr);$i ++) { if($arr[$i] > $value) $new_arr[$j ++] = $arr[$i]; } break; case '<'; for($i = 0, $j = 0;$i < sizeof($arr);$i ++) { if($arr[$i] < $value) $new_arr[$j ++] = $arr[$i]; } break; case '>='; for($i = 0, $j = 0;$i < sizeof($arr);$i ++) { if($arr[$i] >= $value) $new_arr[$j ++] = $arr[$i]; } break; case '<='; for($i = 0, $j = 0;$i < sizeof($arr);$i ++) { if($arr[$i] <= $value) $new_arr[$j ++] = $arr[$i]; } break; case '='; for($i = 0, $j = 0;$i < sizeof($arr);$i ++) { if($arr[$i] == $value) $new_arr[$j ++] = $arr[$i]; } break; case '<>'; for($i = 0, $j = 0;$i < sizeof($arr);$i ++) { if($arr[$i] != $value) $new_arr[$j ++] = $arr[$i]; } break; default; break; } return $new_arr; } function get_new_c_arr_2($arr, $c, $f, $v, $value){ switch($c){ case '>'; for($i = 0, $j = 0;$i < sizeof($arr);$i ++) { if($arr[$i][$f] > $value) $new_arr[$j ++] = $arr[$i][$v]; } break; case '<'; for($i = 0, $j = 0;$i < sizeof($arr);$i ++) { if($arr[$i][$f] < $value) $new_arr[$j ++] = $arr[$i][$v]; } break; case '>='; for($i = 0, $j = 0;$i < sizeof($arr);$i ++) { if($arr[$i][$f] >= $value) $new_arr[$j ++] = $arr[$i][$v]; } break; case '<='; for($i = 0, $j = 0;$i < sizeof($arr);$i ++) { if($arr[$i][$f] <= $value) $new_arr[$j ++] = $arr[$i][$v]; } break; case '='; for($i = 0, $j = 0;$i < sizeof($arr);$i ++) { if($arr[$i][$f] == $value) $new_arr[$j ++] = $arr[$i][$v]; } break; case '<>'; for($i = 0, $j = 0;$i < sizeof($arr);$i ++) { if($arr[$i][$f] != $value) $new_arr[$j ++] = $arr[$i][$v]; } break; default; break; } return $new_arr; } function phpsql_getindexvalues($table, $field, $value, $c, $order, $limit){ global $phpsql_db; $content = PHPSQL_get_contents(PHPSQL_LOC."/".$phpsql_db."/".$table."/index/".$field.".data.php"); $ex = explode("|", $content); for($i = 0, $j = 0;$i < sizeof($ex) - 1;$i ++){ $ex_ex = explode(",", $ex[$i]); $arr[$j][field] = str_replace("'", "", $ex_ex[0]); $arr[$j ++][value] = str_replace("'", "", $ex_ex[1]); } if(isset($c) && isset($value)) $arr = get_new_c_arr_2($arr, $c, field, value, $value); return $arr; } function phpsql_getprimaryfield($table){//get primary key field 's name global $phpsql_db; $dir = PHPSQL_LOC."/".$phpsql_db."/".$table."/structure.data.php"; $content = PHPSQL_get_contents($dir); $ex = explode("(primary)", $content); $ex_ex = explode(",", $ex[0]); $str = str_replace("`", "", $ex_ex[0]); return $str; } function phpsql_getprimaryvalues($table, $c = null, $value = null, $order = null, $limit = null){ global $phpsql_db; $dir = PHPSQL_LOC."/".$phpsql_db."/".$table."/primary.data.php"; $content = PHPSQL_get_contents($dir); $ex = explode("|", $content); for($i = 0, $j = 0;$i < sizeof($ex) - 1;$i ++){ $arr[$j ++] = str_replace("'", "", $ex[$i]); } if(isset($c) && isset($value)) $arr = get_new_c_arr($arr, $c, $value); return $arr; } function phpsql_deletedir($dir){ $dh = opendir($dir); while($file = readdir($dh)){ if($file != "." && $file != ".."){ $fullpath = $dir."/".$file; if(!is_dir($fullpath)){ unlink($fullpath); }else{ phpsql_deletedir($fullpath); } } } closedir($dh); if(rmdir($dir)){ return 1; }else{ return 0; } } function phpsql_altertable($table, $field, $new_field, $default){ global $phpsql_db; $dir = PHPSQL_LOC."/".$phpsql_db."/".$table."/structure.data.php"; $ex = explode("`$field`,'", $content = phpsql_get_contents($dir)); $ex_2 = explode("',(", $ex[1]); $old_default = $ex_2[0]; $content = str_replace("`$field`,'$old_default'", "`$new_field`,'$default'", $content); if(phpsql_write($dir, $content)){ @rename(PHPSQL_LOC."/".$phpsql_db."/".$table."/index/".$field.".data.php", PHPSQL_LOC."/".$phpsql_db."/".$table."/index/".$new_field.".data.php"); return 1; }else return 0; } function phpsql_dropindex($table, $index){ global $phpsql_db; $dir = PHPSQL_LOC."/".$phpsql_db."/".$table."/structure.data.php"; $ex = explode("`$index`", $content = phpsql_get_contents($dir)); $ex_2 = explode("(index)|", $ex[1]); $old_field = "`$index`".$ex_2[0]."(index)|"; $new_field = "`$index`".$ex_2[0]."()|"; $content = str_replace($old_field, $new_field, $content); if(phpsql_write($dir, $content)) { @unlink(PHPSQL_LOC."/".$phpsql_db."/".$table."/index/".$index.".data.php"); return 1; } else return 0; } function phpsql_addindex($table, $index){ global $phpsql_db; $dir = PHPSQL_LOC."/".$phpsql_db."/".$table."/structure.data.php"; $ex = explode("`$index`", $content = phpsql_get_contents($dir)); $ex_2 = explode("()|", $ex[1]); $old_field = "`$index`".$ex_2[0]."()|"; $new_field = "`$index`".$ex_2[0]."(index)|"; $content = str_replace($old_field, $new_field, $content); if(phpsql_write($dir, $content)) { $query = phpsql_query("select * from `$table`"); $num = phpsql_num_rows($query); for($i = 0;$i < $num;$i ++) { $primary_value = phpsql_result($query, $i, phpsql_getprimaryfield($table)); $index_value = phpsql_result($query, $i, $index); $index_in .= "'".$index_value."','".$primary_value."'|"; } phpsql_write(PHPSQL_LOC."/".$phpsql_db."/".$table."/index/".$index.".data.php", $index_in); return 1; } else return 0; } function phpsql_addfield($table, $field, $default, $add){ global $phpsql_db; $dir = PHPSQL_LOC."/".$phpsql_db."/".$table."/structure.data.php"; $content = phpsql_get_contents($dir)."`$field`,'$default',($add)|"; if(phpsql_write($dir, $content)) { if($add == index) phpsql_addindex($table, $field); return 1; } else return 0; } function phpsql_dropfield($table, $field){ global $phpsql_db; $dir = PHPSQL_LOC."/".$phpsql_db."/".$table."/structure.data.php"; $ex = explode("`$field`", $content = phpsql_get_contents($dir)); $ex_2 = explode("|", $ex[1]); $old_field = "`$field`".$ex_2[0]."|"; $content = str_replace($old_field, "", $content); if(phpsql_write($dir, $content)) { @unlink(PHPSQL_LOC."/".$phpsql_db."/".$table."/index/".$field.".data.php"); return 1; } else return 0; } function phpsql_dropdatabase($db){ $dir = PHPSQL_LOC."/".$db; return phpsql_deletedir($dir); } function phpsql_droptable($table){ global $phpsql_db; $dir = PHPSQL_LOC."/".$phpsql_db."/".$table; return phpsql_deletedir($dir); } function phpsql_createtable($table, $fields){ global $phpsql_db; $dir = PHPSQL_LOC; $ex = explode(",", $fields); for($i = 0, $j = 0;$i < sizeof($ex);$i ++) { $ex_2 = explode(" ", $ex[$i]); if($ex_2[2] == primary) {$j ++;if($ex_2[1] && !is_numeric(str_replace("'", "", $ex_2[1])))$j ++;} } if($j != 1) return 0; if(@mkdir($dir."/".$phpsql_db."/".$table)) { @mkdir($dir."/".$phpsql_db."/".$table."/index"); @mkdir($dir."/".$phpsql_db."/".$table."/data"); for($i = 0;$i < sizeof($ex);$i ++) { $ex_2 = explode(" ", $ex[$i]); $structure .= $ex_2[0].",".$ex_2[1].",(".$ex_2[2].")|"; if($ex_2[2] == index) phpsql_write($dir."/".$phpsql_db."/".$table."/index/".str_replace("`", "", $ex_2[0]).".data.php", ""); } phpsql_write($dir."/".$phpsql_db."/".$table."/structure.data.php", $structure); phpsql_write($dir."/".$phpsql_db."/".$table."/primary.data.php", ""); phpsql_write($dir."/".$phpsql_db."/".$table."/rows_count.data.php", "0"); return 1; } } function phpsql_createdatebase($db){ global $phpsql_db; $dir = PHPSQL_LOC; if(@mkdir($dir."/".$db)) return 1; else return 0; } function phpsql_truncate($table){ global $phpsql_db; $index = phpsql_getindex($table); for($i = 0;$i < sizeof($index);$i ++) { $dir = PHPSQL_LOC."/".$phpsql_db."/".$table."/index/".$index[$i][field].".data.php"; phpsql_write($dir, ''); } $dir = PHPSQL_LOC."/".$phpsql_db."/".$table."/primary.data.php"; phpsql_write($dir, ''); $dir = PHPSQL_LOC."/".$phpsql_db."/".$table."/rows_count.data.php"; phpsql_write($dir, '0'); $dir = PHPSQL_LOC."/".$phpsql_db."/".$table."/data"; $handle = opendir($dir); while(($file = readdir($handle)) !== false){ @unlink($dir.'/'.$file); } closedir($handle); return 1; } function phpsql_select($star, $table, $where, $order, $limit, $opentrans = yes){ //"select * from `topic` where `bid` = '1' and `username` = 'mickie' order by `ID` desc limit 10,20" $count = 0; $count_remove = 0; $table = str_replace("`", "", $table); if(strstr($table, ".")){ $table_ex = explode(".", $table); $table_db = $table_ex[0]; phpsql_select_db($table_db); $table = $table_ex[1]; } if(strstr($star, "count(*) as")) { $isstar = 1; $star = str_replace("count(*) as ", "", $star); $star = str_replace("'", "", $star); $star = str_replace(" ", "", $star); }else $isstar = 0; $i = 0; if($where == ''){ //empty query $arr = phpsql_getprimaryvalues($table, null, null, $order, $limit); for($j = 0;$j < sizeof($arr);$j ++) { $arr_i_all[$count] = $arr[$j]; $count ++; } }else{ $ex_ex = explode(" ", $where); for($i = 0, $j = 0;$i < sizeof($ex_ex) - 2;$i += 4) { $field_inner = str_replace("`", "", $ex_ex[$i]);//fields $value_inner = str_replace("'", "", $ex_ex[$i + 2]);//values $c_inner = $ex_ex[$i + 1];//inner symbol $c_outer = $ex_ex[$i - 1];//outer symbol $p = phpsql_getstructure($table, $field_inner); if($p[add] == primary) {//when the field is a primary $arr = phpsql_getprimaryvalues($table, $c_inner, $value_inner, $order, $limit); for($j = 0;$j < sizeof($arr);$j ++) { $arr_i_all[$count] = $arr[$j]; $count ++; } }elseif($p[add] == index){ //when the field is a index if($c_outer == '' || $c_outer == 'or') { $index_values = phpsql_getindexvalues($table, $field_inner, $value_inner, $c_inner, $order, $limit); for($j_2 = 0;$j_2 < sizeof($index_values);$j_2 ++) { $arr_i_all[$count] = $index_values[$j_2]; $count ++; } }elseif($c_outer == 'and'){ $index_values = phpsql_getindexvalues($table, $field_inner, $value_inner, $c_inner, $order, $limit); for($count_2 = 0, $j_2 = 0;$j_2 < sizeof($index_values);$j_2 ++) { for($j_3 = 0;$j_3 < sizeof($arr_i_all);$j_3 ++) { if($arr_i_all[$j_3] == $index_values[$j_2]) { $new_arr_i_all[$count_2 ++] = $index_values[$j_2]; break; } } } $arr_i_all = $new_arr_i_all; } }else{ //when the field is not a primary and not a index if($c_outer != 'and') continue; else{ $arr_i_all_remove[$count_remove][field] = $field_inner; $arr_i_all_remove[$count_remove][value] = $value_inner; $arr_i_all_remove[$count_remove][c] = $c_inner; $count_remove ++; } } } } //set order if($order != '') { $order_ex = explode(" ", $order); $order_field = str_replace("`", "", $order_ex[0]); $order_sc = $order_ex[1]; global $phpsql_db; if($index_data = PHPSQL_get_contents(PHPSQL_LOC."/".$phpsql_db."/".$table."/index/".$order_field.".data.php")){ for($i = 0;$i < sizeof($arr_i_all);$i ++) { //$PHPSQL_data = "'1192441139','1'|'0','2'|'0','3'|'0','4'|'0','5'|'1192443187','6'|"; $index_data_ex = explode(",'".$arr_i_all[$i]."'|", $index_data); $index_data_ex_ex = explode("'", $index_data_ex[0]); $ordervalue = $index_data_ex_ex[sizeof($index_data_ex_ex) - 2]; $all_order_all[$i][ordervalue] = $ordervalue; $all_order_all[$i][IDvalue] = $arr_i_all[$i]; } for($i = 0;$i < sizeof($all_order_all) - 1;$i ++) for($j = $i + 1;$j < sizeof($all_order_all);$j ++) { if($order_sc == desc) { if($all_order_all[$j][ordervalue] > $all_order_all[$i][ordervalue]) { $temp = $all_order_all[$j]; $all_order_all[$j] = $all_order_all[$i]; $all_order_all[$i] = $temp; } }elseif($order_sc == asc) { if($all_order_all[$j][ordervalue] < $all_order_all[$i][ordervalue]) { $temp = $all_order_all[$j]; $all_order_all[$j] = $all_order_all[$i]; $all_order_all[$i] = $temp; } } } for($i = 0;$i < sizeof($all_order_all);$i ++) $arr_i_all[$i] = $all_order_all[$i][IDvalue]; }elseif(phpsql_getprimaryfield($table) == $order_field){ for($i = 0;$i < sizeof($arr_i_all) - 1;$i ++) for($j = $i + 1;$j < sizeof($arr_i_all);$j ++) { if(($order_sc == desc && ($arr_i_all[$j] > $arr_i_all[$i])) || ($order_sc == asc && ($arr_i_all[$j] < $arr_i_all[$i]))) { $temp = $arr_i_all[$j]; $arr_i_all[$j] = $arr_i_all[$i]; $arr_i_all[$i] = $temp; } } } }else{ for($i = 0;$i < sizeof($arr_i_all) - 1;$i ++) for($j = $i + 1;$j < sizeof($arr_i_all);$j ++) { if($arr_i_all[$j] > $arr_i_all[$i]) { $temp = $arr_i_all[$j]; $arr_i_all[$j] = $arr_i_all[$i]; $arr_i_all[$i] = $temp; } } } //END //set limit if($limit != '') { $ex_x = explode(",", str_replace(" ", "", $limit)); $start = $ex_x[1]?$ex_x[0]:0; $next = $ex_x[1]?$ex_x[1]:$ex_x[0]; $tosum = sizeof($arr_i_all); for($i = $start, $j = 0;$i < (($next + $start)>$tosum?$tosum:($next + $start));$i ++) { $newtemp[$j ++] = $arr_i_all[$i]; } $arr_i_all = $newtemp; } //END if($isstar){ $new_arr = array(array()); $new_arr[0][$star] = sizeof($arr_i_all); return $new_arr; }else{ for($i = 0;$i < sizeof($arr_i_all);$i ++) $arr_all[$i] = phpsql_getresult($table, $arr_i_all[$i], $opentrans); //remove somevalue for($i = 0;$i < sizeof($arr_i_all_remove);$i ++) { for($j = 0, $k = 0, $newtemp = array(array());$j < sizeof($arr_all);$j ++) { switch($arr_i_all_remove[$i][c]) { case '>'; if($arr_all[$j][$arr_i_all_remove[$i][field]] > $arr_i_all_remove[$i][value]) $newtemp[$k ++] = $arr_all[$j]; break; case '<'; if($arr_all[$j][$arr_i_all_remove[$i][field]] < $arr_i_all_remove[$i][value]) $newtemp[$k ++] = $arr_all[$j]; break; case '>='; if($arr_all[$j][$arr_i_all_remove[$i][field]] >= $arr_i_all_remove[$i][value]) $newtemp[$k ++] = $arr_all[$j]; break; case '<='; if($arr_all[$j][$arr_i_all_remove[$i][field]] <= $arr_i_all_remove[$i][value]) $newtemp[$k ++] = $arr_all[$j]; break; case '='; if($arr_all[$j][$arr_i_all_remove[$i][field]] == $arr_i_all_remove[$i][value]) $newtemp[$k ++] = $arr_all[$j]; break; case '<>'; if($arr_all[$j][$arr_i_all_remove[$i][field]] != $arr_i_all_remove[$i][value]) $newtemp[$k ++] = $arr_all[$j]; break; default; break; } } } if(sizeof($arr_i_all_remove)) $arr_all = $newtemp; //END return phpsql_setfv($table, $arr_all); } } function phpsql_num_rows($query){ return sizeof($query); } function phpsql_result($query, $num, $field){ return $query[$num][$field]; } function phpsql_insert($table, $fields, $values){ global $ID;global $phpsql_db; $table = str_replace("`", "", $table); if(strstr($table, ".")){ $table_ex = explode(".", $table); $table_db = $table_ex[0]; phpsql_select_db($table_db); $table = $table_ex[1]; } $fields_ex = explode(",", $fields); $values_ex = explode(",", $values); $fields_count = sizeof($fields_ex); $values_count = sizeof($values_ex); if($values_count != $fields_count) phpsql_error("fields and value cannot match."); $ID = PHPSQL_get_contents(PHPSQL_LOC."/".$phpsql_db."/".$table."/rows_count.data.php"); $ID = (int)$ID; $ID ++; $target = PHPSQL_LOC."/".$phpsql_db."/".$table."/data/".$ID.".data.php"; phpsql_write(PHPSQL_LOC."/".$phpsql_db."/".$table."/rows_count.data.php", $ID); $primary_content = PHPSQL_get_contents(PHPSQL_LOC."/".$phpsql_db."/".$table."/primary.data.php"); $primary_content .= "'".$ID."'|"; phpsql_write(PHPSQL_LOC."/".$phpsql_db."/".$table."/primary.data.php", $primary_content); $f_arr = phpsql_getfields($fields); $v_arr = phpsql_getvalues($values); $index = phpsql_getindex($table, $f_arr, $v_arr); $content = phpsql_getcontent($f_arr, $v_arr, $ID); phpsql_write_index($table, $index, $ID); if(phpsql_write($target, $content)) return 1; else return 0; } function phpsql_insert_id(){ global $ID; return $ID; } function phpsql_write_index($table, $index, $ID){ global $phpsql_db; for($i = 0;$i < sizeof($index);$i ++){ $target = PHPSQL_LOC."/".$phpsql_db."/".$table."/index/".$index[$i][field].".data.php"; $content = @PHPSQL_get_contents($target); $content .= "'".$index[$i][value]."','".$ID."'|"; phpsql_write($target, $content); } } function phpsql_getindex($table, $f = null, $v = null){ global $phpsql_db; $content = PHPSQL_get_contents(PHPSQL_LOC."/".$phpsql_db."/".$table."/structure.data.php"); $ex = explode("|", $content); for($i = 0, $j = 0;$i < sizeof($ex) - 1;$i ++) { $ex_ex = explode(",", $ex[$i]); if($ex_ex[2] == "(index)") { $arr[$j][field] = str_replace("`", "", $ex_ex[0]); if(isset($f) && isset($v)) for($k = 0;$k < sizeof($f);$k ++) { if($f[$k] == str_replace("`", "", $ex_ex[0])) {$arr[$j][value] = $v[$k];break;} } if(!isset($arr[$j][value])) { $p = phpsql_getstructure($table, $arr[$j][field]); $arr[$j][value] = $p['default']; } $j ++; } } return $arr; } function phpsql_error($str){ die($str); } function phpsql_getcontent($fields, $values, $ID = 0){ for($i = 0;$i < sizeof($fields);$i ++) $content .= "`".$fields[$i]."`'".$values[$i]."'".($i == (sizeof($fields)-1)?'':'{|n|}'); $content = str_replace("NULL", $ID, $content); return $content; } function phpsql_getfields($fields){ $ex = explode(",", $fields); $count = sizeof($ex); if(!$count) return 0; for($i = 0;$i < $count;$i ++) { $arr[$i] = str_replace("`", "", str_replace(" ", "", $ex[$i])); } return $arr; } function phpsql_getvalues($values){ $ex = explode(",", $values); $count = sizeof($ex); if(!$count) return 0; for($i = 0;$i < $count;$i ++) { $arr[$i] = str_replace("'", "", str_replace(" ", "", $ex[$i])); } return $arr; } function phpsql_write($loc, $content){ $content = "<?php $"."PHPSQL_data = \"".$content."\"; ?>"; $fp = @fopen($loc, "wb"); if(@fwrite($fp, $content) && @fclose($fp)) return 1; else return 0; } function PHPSQL_get_contents($target){ @include $target; return $PHPSQL_data; } ?>